<!--
 * @Author: dengyl
 * @Date: 2022-01-14 17:09:19
 * @LastEditTime: 2022-08-10 18:03:13
 * @LastEditors: 詹真琦(legendryztachi@gmail.com)
 * @Description: 多彩磁贴
 * @FilePath: \code\ctp-static-portal-front\src\components\tabs\components\color-magnet.vue
-->
<template>
  <div class="color-magnet">
    <div class="container" ref="$container">
      <ul>
        <li v-for="(item, index) in times" :key="index">
          <div
            class="big text-over"
            :class="{ 'is-active': currentIndex == index }"
            @click="selectHandle(index)"
            v-if="isShowBig(index)"
            :style="`background-color:${dataList[index].sectionData.options.sectionTabBgColor}`"
            ><span>{{ dataList[index].name }}</span>
            <div
              class="arrow"
              :style="`border-top-color:${dataList[index].sectionData.options.sectionTabBgColor}`"
            ></div>
            <div class="big-icon">
              <Icon :icon="dataList[index].sectionData.options.sectionTabIcon" :size="50" />
            </div>
          </div>
          <div class="small" v-else>
            <div
              :class="[{ 'is-active': currentIndex == index }]"
              @click="selectHandle(index)"
              :style="`background-color:${dataList[index].sectionData.options.sectionTabBgColor}`"
            >
              <div
                class="arrow"
                :style="`border-top-color:${dataList[index].sectionData.options.sectionTabBgColor}`"
              ></div
              ><span class="text-over">{{ dataList[index].name }}</span></div
            >
            <div
              :class="[{ 'is-active': currentIndex == index + indexDeviation }]"
              @click="selectHandle(index + indexDeviation)"
              class="bottom"
              :style="`background-color:${
                dataList[index + indexDeviation].sectionData.options.sectionTabBgColor
              }`"
            >
              <div
                class="arrow"
                :style="`border-top-color:${
                  dataList[index + indexDeviation].sectionData.options.sectionTabBgColor
                }`"
              ></div
              ><span class="text-over">{{ dataList[index + indexDeviation].name }}</span></div
            >
          </div>
        </li>
      </ul>
    </div>
  </div>
</template>
<script>
  export default {
    name: 'ColorMagnet',
    props: {
      dataList: {
        type: Array,
      },
      selectTab: {
        type: Function,
      },
    },
    data() {
      return {
        defaultW: 157,
        // 当前容器可视的最大值
        maxCount: 0,
        // 需要显示大样式li的个数
        bigLi: 0,
        // 循环次数
        times: [],
        // index偏移量
        indexDeviation: 0,
        currentIndex: 0,
      };
    },
    created() {
      setTimeout(() => {
        const len = this.dataList.length;
        const containerW = this.$refs.$container?.clientWidth;
        this.maxCount = parseInt(containerW / this.defaultW);
        const doubleCount = this.maxCount * 2;
        // 计算需要显示多少个高度为70的li标签
        if (len <= this.maxCount) {
          this.bigLi = len;
        } else if (len > this.maxCount && len <= doubleCount) {
          this.bigLi = this.maxCount * 2 - len;
        } else if (len % 2 === 1) {
          // 如果超过最大值且是奇数，要变为偶数
          this.bigLi = 1;
        } else {
          this.bigLi = 0;
        }

        this.times = Array.from({ length: this.bigLi + (len - this.bigLi) / 2 });
        this.indexDeviation = this.times.length - this.bigLi;
      }, 0);
    },
    methods: {
      /**
       * @description: 是否展示大li
       */
      isShowBig(i) {
        return i < this.bigLi;
      },
      /**
       * @description: 切换
       */
      selectHandle(index) {
        // this.$emit('update', index);
        this.selectTab(index);
        this.currentIndex = index;
      },
    },
  };
</script>
<style scoped lang="scss">
  .color-magnet {
    .container {
      width: 100%;

      .is-active {
        & > .arrow {
          position: absolute;
          bottom: 2px;
          left: 25px;
          width: 0;
          height: 0;
          border-top: 8px solid;
          border-right: 8px solid transparent;
          border-left: 8px solid transparent;
        }
      }

      > ul {
        padding: 5px;
        margin: 0;
        overflow: auto;
        white-space: nowrap;
        list-style: none;

        > li {
          position: relative;
          display: inline-block;
          width: 147px;
          height: 80px;
          margin: 0 5px;
          font-size: 14px;
          color: #fff;
          text-align: center;
          vertical-align: top;

          .text-over {
            display: block;
            overflow: hidden;
            text-indent: 5px;
            text-overflow: ellipsis;
            white-space: nowrap;
          }

          > .big {
            width: 100%;
            height: 70px;
            line-height: 70px;
            border-radius: 6px;

            &:hover {
              cursor: pointer;
            }

            > .big-icon {
              position: absolute;
              right: 0;
              bottom: 9px;
              width: 50px;
              height: 50px;
              font-size: 50px;
              line-height: 0;
              color: #fff;
              opacity: 0.3;
              filter: alpha(opacity=30);
            }
          }

          > .small {
            > div {
              position: relative;
              width: 100%;
              height: 30px;
              line-height: 30px;
              background-color: blueviolet;
              border-radius: 6px;

              &:hover {
                cursor: pointer;
              }
            }

            > .is-active {
              & > .arrow {
                bottom: -8px;
              }
            }

            > .bottom {
              margin: 10px 0;
            }
          }
        }

        // 解决Chrome下的滚动条样式
        &::-webkit-scrollbar {
          /* 滚动条整体部分，其中的属性有width,height,background,border等（就和一个块级元素一样）（位置1） */
          width: 2px;
          height: 3px;
        }

        &::-webkit-scrollbar-button {
          /* 滚动条两端的按钮（位置2） */
          display: none;
        }

        &::-webkit-scrollbar-track-piece {
          /* 内层轨道，滚动条中间部分（位置4） */
          width: 8px;
          background-color: #f5f5f5;
          border-radius: 6px;
          visibility: hidden;
        }

        &::-webkit-scrollbar-thumb {
          /* 滚动条里面可以拖动的那部分（位置5） */
          height: 50px;
          background: #bec7d5;
          border-radius: 6px;
          visibility: hidden;
        }

        &:hover {
          &::-webkit-scrollbar-track-piece,
          &::-webkit-scrollbar-thumb {
            visibility: visible;
          }
        }
      }
    }
  }
</style>
